package DMPS::Gm;
use Dancer2;
use Dancer2::Plugin::Database;

our $VERSION = '0.1';

get '/' => sub {
    template 'welcome';
};

get '/gma' => sub {
	my $r=int(rand()*11);
	open FILE, "<", "gen$r.dat" or die $!;
	my $string = join("", <FILE>);
	close FILE;
	chomp($string);
	#$r=0;
	#my $num=45;#get from database, lock etc
	#say $r,$num;
    template 'sim', {'datnum' => $r, 'gmt' => 0, 'datgen' => $string};
};

get '/gmb' => sub {
	my $r=int(rand()*11);
	$r=9;
	open FILE, "<", "gen$r.dat" or die $!;
	my $string = join("", <FILE>);
	close FILE;
	chomp($string);
	#$r=0;
	#my $num=45;#get from database, lock etc
	#say $r,$num;
    template 'sim', {'datnum' => $r, 'gmt' => 1, 'datgen' => $string};
};

post '/thanks' => sub {
    #template 'thanks';
	my $sth = database->prepare('INSERT INTO dt VALUES (?,?,?)') or die "Couldn't prepare statement: " . database->errstr;
	$sth->execute(params->{'typ'},params->{'num'},params->{'pls'});
	redirect '/thanks';
};

get '/thanks' => sub {
    template 'thanks';
};

true;
